#include "downloadwidget.h"
#include "ui_downloadwidget.h"

DownloadWidget::DownloadWidget(QWidget *parent) :
	QFrame(parent),
	ui(new Ui::DownloadWidget)
{
	ui->setupUi(this);
}

DownloadWidget::~DownloadWidget()
{
	delete ui;
}

void DownloadWidget::setUrl(QString url)
{
	ui->txtUrl->setText(url);
}

void DownloadWidget::setMime(QString mime)
{
	ui->txtMineType->setText(mime);
}

void DownloadWidget::setSize(qint64 size)
{
	QString sizeString;
	if (size < 0) {
		sizeString = "Unknown";
	} else if (size < 1.0e3) {
		sizeString = QString("%1 B").arg(size);
	} else if (size < 1.0e6) {
		sizeString = QString("%1 KB").arg(size / 1.0e3, 0, 'f', 2);
	} else if (size < 1.0e9) {
		sizeString = QString("%1 MB").arg(size / 1.0e6, 0, 'f', 2);
	} else if (size < 1.0e12) {
		sizeString = QString("%1 GB").arg(size / 1.0e9, 0, 'f', 2);
	} else if (size < 1.0e15) {
		sizeString = QString("%1 TB").arg(size / 1.0e12, 0, 'f', 2);
	} else if (size < 1.0e18) {
		sizeString = QString("%1 PB").arg(size / 1.0e15, 0, 'f', 2);
	} else {
		sizeString = QString("%1 EB").arg(size / 1.0e18, 0, 'f', 2);
	}
	ui->txtSize->setText(sizeString);
}

void DownloadWidget::setProgress(int percent)
{
	ui->progressBar->setValue(percent);
}

void DownloadWidget::setProgress(qint64 bytesRead, qint64 bytesTotal)
{
	if (bytesTotal <= 0) {
		setSize(-1);
		setProgress(50);
	} else {
		setSize(bytesTotal);
		setProgress((bytesRead * 100)/ bytesTotal);
	}
}
